package network.盗版qq.qqclient.service;

import lombok.AllArgsConstructor;
import lombok.Getter;
import network.盗版qq.comment.Message;
import network.盗版qq.comment.MessageType;

import java.io.ObjectInputStream;
import java.net.Socket;

/**
 * @author mubei
 * @date 2021/12/6
 * @description
 */
@AllArgsConstructor
@Getter
public class ClientConnectServerThread {

    //  线程需要持有Socket
    private Socket socket;

    public void run() {
        // 因为Thread需要后台和服务器通信   因此需要while循环
        while (true) {
            try {
                System.out.println("客户端线程  等到从读取冲服务器发送的消息");
                ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                Message msg = (Message) ois.readObject();
                // 后面要使用msg
                if (msg.getMesType().equals(MessageType.RETURN_ONLINE_FRIEND)) {
                    // 取出在线列表
                    String[] onLineUsers = msg.getContent().split(" ");
                    System.out.println("\n======当前用户列表======");
                    for (int i = 0; i < onLineUsers.length; i++) {
                        System.out.println("用户: " + onLineUsers[i]);
                    }
                } else if (msg.getMesType().equals(MessageType.COMM_MES)) {
                    System.out.println("\t"+msg.getSender()+" "+msg.getSenTime()+" :"+msg.getContent());
                } else if(msg.getMesType().equals(MessageType.COMM_MES_TO_ALL)){
                    System.out.println("\t"+msg.getSender()+"对大家说:"+msg.getContent());
                }
                else {
                    System.out.println("其他类型  再处理");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


}
